package com.googlecode.stockhamster.stockstat;

import java.sql.Timestamp;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;

public class StockHistory {
	public static final Long interval = 60000L;

	private Integer stockId;
	private Long price;
	private Long volumes;
	private Timestamp time;
	private int ID;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public StockHistory() {
	}

	public StockHistory(Integer stockID, Long price, Long volumes,
			Timestamp time) {
		this.stockId = stockID;
		this.price = price;
		this.volumes = volumes;
		this.time = time;
	}

	public static void stockInit(Integer stockId, Long price) {
		StockHistory last = new StockHistory();
		last.setStockId(stockId);
		last.setPrice(price);
		last.setVolumes(0L);
		last.setTime(new Timestamp(new Date().getTime() / interval * interval));
		last.insert();
	}

	public static Boolean stockChange(Integer stockId, Long price, Long volumes) {
		StockHistory last = refresh(stockId);
		last.setPrice((last.getPrice() * last.getVolumes() + price * volumes)
				/ (last.getVolumes() + volumes));
		last.setVolumes(last.getVolumes() + volumes);
		return last.save();
	}

	public static StockHistory refresh(Integer stockId) {
		Timestamp time = new Timestamp(new Date().getTime() / interval
				* interval);

		StockHistory last = getLastHistory(stockId);
		while (time.getTime() - last.getTime().getTime() > interval / 2) {
			Long lastPrice = last.getPrice();
			Timestamp newTime = new Timestamp(last.getTime().getTime()
					+ interval);

			last = new StockHistory();
			last.setStockId(stockId);
			last.setPrice(lastPrice);
			last.setVolumes(0L);
			last.setTime(newTime);
			last.insert();
		}
		return last;
	}

	// Sorted by date desc
	// items xiang~
	public static StockHistory[] getHistorys(Integer stockId, Integer items) {
		ArrayList<StockHistory> tempAccount = new ArrayList<StockHistory>();
		try {
			String sqlGetAccountList = "select * from StockHistory where stockId=? order by time DESC";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setInt(1, stockId);
			rs = state.executeQuery();
			while (rs.next()) {
				StockHistory ac = new StockHistory();
				ac = new StockHistory(rs.getInt(1), rs.getLong(2), rs
						.getLong(3), rs.getTimestamp(4));
				ac.setID(rs.getInt(5));
				tempAccount.add(ac);
			}
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
			}
		}
		int tempSize = Math.min(items, tempAccount.size());
		StockHistory[] targetList = new StockHistory[tempSize];
		for (int i = 0; i < tempSize; i++)
			targetList[i] = tempAccount.get(i);
		return targetList;
	}

	public static StockHistory getLastHistory(Integer stockId) {
		StockHistory ret = null;
		try {
			String sqlGetAccountList = "select * from StockHistory where stockId=? order by time DESC";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setInt(1, stockId);
			rs = state.executeQuery();
			if (rs.next()) {
				ret = new StockHistory(rs.getInt(1), rs.getLong(2), rs
						.getLong(3), rs.getTimestamp(4));
				ret.setID(rs.getInt(5));
			}
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (Exception e) {
			}
		}
		return ret;
	}

	public Boolean save() {
		boolean flag = false;
		try {
			String sqlSetStockAccount = "update StockHistory set stockId=?, price=?, volumes=?, time=? where ID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlSetStockAccount);
			state.setInt(1, this.stockId);
			state.setLong(2, this.price);
			state.setLong(3, this.volumes);
			state.setTimestamp(4, new java.sql.Timestamp(this.time.getTime()));
			state.setInt(5, this.ID);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			try {
				if (state != null) {
					state.close();
				}
			} catch (Exception e) {
			}
		}
		return flag;
	}

	public Boolean insert() {
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into StockHistory(stockId, price, volumes, time) values(?,?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setInt(1, this.stockId);
			state.setLong(2, this.price);
			state.setLong(3, this.volumes);
			state.setTimestamp(4, new java.sql.Timestamp(this.time.getTime()));
			state.executeUpdate();
			flag = true;
			state.close();
			String sqlTempSelect = "select max(id) from StockHistory";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlTempSelect);
			rs = state.executeQuery();
			rs.next();
			this.setID(rs.getInt(1));
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			try {
				if (state != null) {
					state.close();
				}
			} catch (Exception e) {
			}
		}
		return flag;
	}

	public Integer getStockId() {
		return stockId;
	}

	public void setStockId(Integer stockId) {
		this.stockId = stockId;
	}

	public Long getPrice() {
		return price;
	}

	public void setPrice(Long price) {
		this.price = price;
	}

	public Timestamp getTime() {
		return time;
	}

	public void setTime(Timestamp time) {
		this.time = time;
	}

	public void setVolumes(Long volumes) {
		this.volumes = volumes;
	}

	public Long getVolumes() {
		return volumes;
	}

	public int getID() {
		return ID;
	}

	public void setID(int ID) {
		this.ID = ID;
	}
}
